<?php
/**
 * Created by PhpStorm
 * User: lcz
 * Date: 2019/10/23
 * Time: 10:39
 * 总部
 */

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\AgentSendTips;
use app\common\model\AgentShop;
use app\admin\model\User as UserModel;
use app\common\model\Order;
use think\Cache;
use think\Db;

class Headquarters extends Api {
	
	/**
	 * 统计
	 */
	public function statistics() {
		$level = $this->auth->level;
		$userId = $this->auth->id;
		$is_admin = false;
		if($level != UserModel::LEVEL_HEADQUARTERS) {
			$adminLevel = \app\common\model\AgentAdmin::getAdminLevel($userId);
			if(!$adminLevel || $adminLevel['role']) {
				$this->error('权限不足');
			}
			
			$is_admin = true;
		}
		
		//订单数
		$orderWhere = ['status' => ['in', [Order::STATUS_SIGN, Order::STATUS_SUCCESS, Order::STATUS_WAIT_SHIPPING, Order::STATUS_WAIT_SIGN]]];
		$order = Db::name('order')
			->where($orderWhere)
			->field('count(id) as c, sum(number) as number, sum(amount) as amount')
			->find();
		
		//统计各角色数量
		$count = Db::name('agentShop')
			->where([
				'status' => AgentShop::STATUS_Y,
			])
			->group('role')
			->column('count(*)', 'role');
		
		
		//推广员
		$promotion = Db::name('promotion')->where(['status' => \app\common\model\Promotion::STATUS_Y])->count();
		
		//待奖励数量
//		$rewardNumber = Db::name('user')
//			->where(['level' => ['in', [UserModel::LEVEL_WAREHOUSE, UserModel::LEVEL_CENTER]]])
//			->sum('reward_number');
		$rewardNumber = Db::name('agentShop')
			->sum('gift_number');
		
		
		//未发货提醒数量
		$sendNumber = Db::name('agentSendTips')->where(['user_id' => $this->auth->id, 'status' => AgentSendTips::STATUS_WAIT])
			->count();
		
		$userNumber = Db::name('user')->count();
		
		$destory_number = Db::name('order')
			->where([
//				'sign_type' => Order::SIGN_TYPE_SELF,
				'status' => Order::STATUS_SUCCESS
			])
			->sum('number');
		
		$this->success('查询成功', [
			'order_number' => $order['c'],
			'sale_number' => $order['number'] > 0 ? round($order['number'], 2) : 0,
			'order_amount' => $order['amount'] > 0 ? round($order['amount'], 2) : 0,
			'theater' => $count[UserModel::LEVEL_THEATER] ?? 0,
			'center' => $count[UserModel::LEVEL_CENTER] ?? 0,
			'warehouse' => $count[UserModel::LEVEL_WAREHOUSE] ?? 0,
			'shop' => $count[UserModel::LEVEL_SHOP] ?? 0,
			'promotion' => $promotion,
			'reward_number' => $rewardNumber,
			'send_number' => $sendNumber,
			'user_number' => $userNumber,
			'is_admin' => $is_admin,
			'destory_number' => $destory_number
		]);
	}
	
	
	/**
	 * 下级所有列表
	 */
	public function agentMap() {
		$key = $this->request->path().md5($this->request->controller().$this->request->action());
		if(!$list = Cache::get($key)) {
			$where =[
				'status' => AgentShop::STATUS_Y,
			];
			
			$obj = Db::name('agentShop');
			$field = 'id, name, mobile, lat, lng, role';
			
			$agents = $obj
				->where($where)
				->field($field)
				->select();
			
			$list = [];
			if($agents) {
				$index = 0;
				foreach ($agents as $agent) {
					$list[] = [
						'id' => $index,
						'latitude' => $agent['lat'],
						'longitude' => $agent['lng'],
						'iconPath' => $agent['role'] == 3 ?'/static/location_shop.png' :'/static/location.png',
						'width' => 30,
						'height' => 30,
						'title' => $agent['name'].'('.$agent['mobile'].')',
					];
					$index ++;
				}
			}
			Cache::set($key, $list, 600);
		}
		
		$this->success('查询成功', $list);
	}
	
	/**
	 * 排行
	 */
	public function ranking() {
		$role = $this->request->get('role');
		
		
		if($role == UserModel::LEVEL_PROMOTION) {
			//推广员
			$rows = Db::name('promotion')->alias('p')
				->join('user u', 'u.id = p.user_id', 'left')
				->order(['p.sale_number' => 'desc'])
				->where([
					'p.sale_number' => ['gt', 0],
					'p.status' => \app\common\model\Promotion::STATUS_Y])
				->limit(10)
				->field('p.name, u.nickname, p.sale_number')
				->select();
		} else {
			$rows = Db::name('agentShop')->alias('p')
				->join('user u', 'u.id = p.user_id', 'left')
				->order(['p.sale_number' => 'desc'])
				->where([
					'p.role' => $role,
					'p.sale_number' => ['gt', 0],
					'p.status' => AgentShop::STATUS_Y])
				->limit(10)
				->field('u.nickname, p.sale_number, p.name')
				->select();
		}
		
		$this->success('查询成功', $rows);
	}
}
